#pragma once

#include <sys/epoll.h>
#include <iostream>

class Epoll
{
    const static int gsize = 128;
public:
    int CreateEpoll()
    { // 创建epoll模型
        _epfd = epoll_create(gsize);
        if(_epfd < 0) exit(5);

        return _epfd;
    }

    void AddSockToEpoll(int sock, uint32_t events)
    { // 将sock添加到epoll中
        epoll_event eevent;
        eevent.data.fd = sock;
        eevent.events = events;
        epoll_ctl(_epfd, EPOLL_CTL_ADD, sock, &eevent);
    }

    void DelSockFromEpoll(int sock)
    { // 删除Epoll中对sock的关心
        epoll_ctl(_epfd, EPOLL_CTL_DEL, sock, nullptr);
    }

    void ModEpoll(int sock, uint32_t events)
    { // 修改Epoll中关心的事件
        events |= EPOLLET;
        epoll_event eevent;
        eevent.data.fd = sock;
        eevent.events = events;

        epoll_ctl(_epfd, EPOLL_CTL_MOD, sock, &eevent);
    }

    int WaitEpoll(epoll_event *revents, int revnum, int timeout)
    { // 等sock就绪
        return epoll_wait(_epfd, revents, revnum, timeout);
    }

public:
    int _epfd;
};